<template>
  <div class="login_Container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="ruleFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              placeholder="请输入帐号"
              v-model="loginForm.username"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              placeholder="请输入密码"
              type="password"
              show-password
              v-model="loginForm.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              @click="onSubmit"
              class="login_btn"
              type="primary"
              :loading="loading"
            >
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
//引入小仓库
import useUserStore from '@/store/modules/user'
//引入路由
import { useRouter, useRoute } from 'vue-router'
//引入通知组件
import { ElNotification } from 'element-plus'
//引入时间工具方法
import { GetTimeHello } from '@/utils/time'
//定义是否加载
let loading = ref(false)
//获取小仓库
let useStore = useUserStore()
//获取路由器
let $router = useRouter()

let $route = useRoute()

//收集用户登陆信息
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
//自定义校验规则
const validateUserName = (rule: any, value: any, callback: any) => {
  if (value.length < 5) {
    callback(new Error('帐号长度不能小于5位'))
  } else {
    callback()
  }
}
//获取el-from组件
let ruleFormRef = ref()
//定义表单校验需要配置对象
const rules = {
  //规则对象属性：
  //required:代表这个字段务必要校验的
  //min:文本长度最少为多少位
  //max:文本长度最多为多少位
  //message:错误的提示信息
  //trigger:触发校验表单的时机
  username: [{ validator: validateUserName, trigger: 'change' }],
  password: [
    {
      required: true,
      min: 6,
      message: '密码最少为6位',
      trigger: 'change',
    },
  ],
}
const onSubmit = async () => {
  //保证全部校验通过才提交
  await ruleFormRef.value.validate()
  //显示加载效果
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    //登陆成功，加载效果消失
    loading.value = false
    let redirect: any = $route.query.redirect
    //编辑式导航跳转到展示数据首页
    $router.push(redirect ? redirect : '/')
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi, ${GetTimeHello()}好`,
    })
  } catch (error) {
    //登陆失败，加载效果消失
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
</script>

<style lang="scss" scoped>
.login_Container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 60%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    top: 30vh;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
